package com.yilijk.base.utils;

import android.app.Activity;
import android.content.Intent;

import java.util.ArrayList;

public class ActivityManagerUtils {


    /**
     * 埋点字段 公共参数
     * 上一个访问页面
     */
    public static String referPage = "";

    private static ArrayList<Activity> activities = new ArrayList<>();

    public static void addActivities(Activity activity) {
        try {
            if (activities != null && activities.size() > 0) {
                Activity activity1 = activities.get(activities.size() - 1);
                String refer = activity1.getClass().getSimpleName();
                if ("RNActivity".equals(refer)) {
                    StringBuilder pageName = new StringBuilder("");
                    Intent intent = activity1.getIntent();
                    pageName.append(intent.getStringExtra("intent_key_plugin_id"));
                    pageName.append("_").append(intent.getStringExtra("intent_key_module_id"));
                    pageName.append("_").append(intent.getStringExtra("intent_key_component_name"));
                    refer = pageName.toString();
                }
                referPage = refer;
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        activities.add(activity);
    }

    public static void addActivities(int p, Activity activity) {
        activities.add(p, activity);
    }

    public static void removeActivities(Activity activity) {
        try {
            String refer = activity.getClass().getSimpleName();
            if ("RNActivity".equals(refer)) {
                StringBuilder pageName = new StringBuilder("");
                Intent intent = activity.getIntent();
                pageName.append(intent.getStringExtra("intent_key_plugin_id"));
                pageName.append("_").append(intent.getStringExtra("intent_key_module_id"));
                pageName.append("_").append(intent.getStringExtra("intent_key_component_name"));
                refer = pageName.toString();
            }
            referPage = refer;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        activities.remove(activity);
    }

    public static void removeActivities(int p) {
        activities.remove(p);
    }

    public static void finishAllActivity() {
        for (int i = 0; i < activities.size(); i++) {
            if (activities.get(i) != null && !activities.get(i).isFinishing()) {
                activities.get(i).finish();
            }
        }
        activities.clear();
    }

    public static Activity getLastestActivity() {
        if (activities.size() != 0) {
            return activities.get(activities.size() - 1);
        }
        return null;
    }


}
